package cn.tedu.property.management.controller;

import cn.tedu.property.management.pojo.dto.OwnerAddNewDTO;
import cn.tedu.property.management.pojo.dto.OwnerUpdateDTO;
import cn.tedu.property.management.pojo.vo.OwnerListVO;
import cn.tedu.property.management.pojo.vo.OwnerStandardVO;
import cn.tedu.property.management.service.IOwnerService;
import cn.tedu.property.management.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/owner")
@Slf4j
@Api(tags = "业主管理模块")
public class OwnerController {
    @Autowired
    private IOwnerService ownerService;
    @PostMapping("/add-new")
    @ApiOperation("新增业主")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(OwnerAddNewDTO ownerAddNewDTO){
        ownerService.insert(ownerAddNewDTO);
        return JsonResult.ok("新增业主信息成功！");
    }
    @PostMapping("/update/{id}")
    @ApiOperation("修改业主")
    @ApiOperationSupport(order = 200)
    public JsonResult update(
            @PathVariable Long id, OwnerUpdateDTO ownerUpdateDTO){
        ownerService.updateById(id,ownerUpdateDTO);
        return JsonResult.ok("修改业主信息成功！");
    }
    @PostMapping("delete/{id}")
    @ApiOperation("根据Id删除业主信息")
    @ApiOperationSupport(order = 300)
    public JsonResult delete(@PathVariable Long id){
        ownerService.deleteById(id);
        return JsonResult.ok("删除业主信息成功！");
    }
    @GetMapping("/select/name")
    @ApiOperation("根据姓名查询业主信息")
    @ApiOperationSupport(order = 400)
    public JsonResult<OwnerStandardVO> selectByName(String name){
        OwnerStandardVO ownerByName = ownerService.getOwnerByName(name);
        return JsonResult.ok(ownerByName);
    }
    @GetMapping("/select/phone")
    @ApiOperation("根据业主手机号查询业主信息")
    @ApiOperationSupport(order = 410)
    public JsonResult<OwnerStandardVO> selectByPhone(String phone){
        OwnerStandardVO ownerByPhone = ownerService.getOwnerByPhone(phone);
        return JsonResult.ok(ownerByPhone);
    }
    @GetMapping("/select/room-number")
    @ApiOperation("根据业主房间号查询业主信息")
    @ApiOperationSupport(order = 420)
    public JsonResult<OwnerStandardVO> selectByRoomNumber(String roomNumber){
        OwnerStandardVO ownerByRoomNumber = ownerService.getOwnerByRoomNumber(roomNumber);
        return JsonResult.ok(ownerByRoomNumber);
    }
    @GetMapping("/select/{id}")
    @ApiOperation("根据业主id查询业主信息")
    @ApiOperationSupport(order = 430)
    public JsonResult<OwnerStandardVO> selectById(@PathVariable Long id){
        OwnerStandardVO ownerById = ownerService.getOwnerById(id);
        return JsonResult.ok(ownerById);
    }
    @GetMapping("/select")
    @ApiOperation("查询所有业主信息")
    @ApiOperationSupport(order = 440)
    public JsonResult<List<OwnerListVO>> selectAllOwner(){
        List<OwnerListVO> allOwner = ownerService.getAllOwner();
        return JsonResult.ok(allOwner);
    }

}
